﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using eCommerce.DataAccess;

namespace eCommerce.CMS.CustomAdmin.Controls.Subscribers
{
    public partial class Edit : System.Web.UI.UserControl
    {
        Xcms_Subscriber _newsl;
        Int32 _pageID;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["PageID"] != null)
            {
                if (Int32.TryParse(Request.QueryString["PageID"], out _pageID))
                {
                }
                else
                {
                    Response.Redirect("~/");
                }
            }
            if (!Page.IsPostBack)
            {

                using (WebsiteDataContext adatabaseDataContext = new WebsiteDataContext())
                {
                    try
                    {
                        _newsl = (from x in adatabaseDataContext.Xcms_Subscribers
                                 where x.id == _pageID
                                 select x).Take(1).SingleOrDefault();
                        if (_newsl != null)
                        {
                            tbxName.Text = _newsl.name;
                            tbxEmail.Text = _newsl.email;
                            ddlStatus.SelectedValue = _newsl.status;
                        }

                        //lblTest.Text = "Subscriber has been updated";
                    }
                    catch (Exception ex)
                    {
                        lblTest.Text = "No user found. Message: " + ex.Message;
                    }
                }
            }
        }
        protected void EditBtn_Click(object sender, EventArgs e)
        {
            if (Int32.TryParse(Request.QueryString["PageID"], out _pageID))
            {
                using (WebsiteDataContext ndc = new WebsiteDataContext())
                {

                    _newsl = (from p in ndc.Xcms_Subscribers
                              where p.id == _pageID
                             select p).Single();

                    _newsl.name = tbxName.Text;
                    _newsl.email = tbxEmail.Text;
                    _newsl.status = ddlStatus.SelectedValue;

                    ndc.SubmitChanges();
                    if (_newsl.id == 0)
                    {
                        lblTest.Text = "Error";
                    }
                    else
                    {
                        lblTest.Text = "Subscriber updated";
                    }



                }
            }
        }
    }
}